import { queryCompanies } from '@/pages/SystemUser/service';
import { verifyId } from '@/utils/checkIdNumber';
import { useModel } from 'umi';
import { Role } from '@/utils/types.d';
import { zhReg } from '@/utils/utils';
import { ModalForm, ProFormSelect, ProFormText } from '@ant-design/pro-components';
import { message } from 'antd';
import { updateTollCollector } from '../service';
import type { TollCollectorTableItem, UpdateTollCollector } from '../types';
// import { queryParkinglotNames } from '@/pages/Parking/Spot/service';

export default (props: { reload: () => void; toller: TollCollectorTableItem }) => {
  const { toller, reload } = props;
  const { initialState } = useModel('@@initialState');
  const userRole = initialState?.currentUser?.role;
  const {
    account,
    name,
    ownerId,
    identificationNumber,
    phoneNumber,
    // parkingLotId,
    // parkingLots,
    _id,
    ownerInfo,
    jobNo,
  } = toller;
  return (
    <ModalForm<UpdateTollCollector>
      title={`编辑收费人员 ${jobNo}`}
      layout="horizontal"
      width="40vw"
      modalProps={{ destroyOnClose: true }}
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 14 }}
      initialValues={{
        account: account,
        password: '',
        name: name,
        ownerId: ownerId,
        identificationNumber: identificationNumber,
        phoneNumber: phoneNumber,
        // parkingLotId: parkingLotId,
        // parkingLots: parkingLots?.map((item) => item.parkingLotId),
      }}
      trigger={<a>编辑</a>}
      onFinish={async (values) => {
        const res = await updateTollCollector(_id, values);
        if (res.success) {
          message.success({
            content: res.message,
            key: 'update_toller',
          });
          reload();
          return true;
        }
        message.error({
          content: res.message,
          key: 'update_toller',
        });
        return false;
      }}
    >
      <ProFormText disabled required label="员工账号" placeholder="设置账号" name="account" />
      <ProFormText
        required
        label="员工姓名"
        placeholder="请填写真实姓名"
        name="name"
        rules={[
          {
            validator: (_, val) => {
              if (!val || val.length < 2)
                return Promise.reject(new Error('用户名称长度不得低于2位'));
              if (!zhReg.test(val)) return Promise.reject(new Error('请填写真实的中文姓名'));
              return Promise.resolve(val);
            },
          },
        ]}
      />
      <ProFormText.Password
        label="登录密码"
        placeholder="设置登录密码"
        name="password"
        rules={[
          {
            validator: (_, val) => {
              if (val && zhReg.test(val)) return Promise.reject(new Error('登录密码不符合要求'));
              if (val && val.length < 6)
                return Promise.reject(new Error('用户密码长度不得低于6位'));
              if (val === '123456') return Promise.reject(new Error('密码强度过低'));
              return Promise.resolve(val);
            },
          },
        ]}
      />
      <ProFormText
        name="identificationNumber"
        label="身份证号"
        placeholder="请输入身份证号"
        rules={[
          {
            validator: (_, val) => {
              if (val) {
                const res = verifyId(String(val));
                if (!res.enable) return Promise.reject(new Error(res.message));
              }
              return Promise.resolve(val);
            },
          },
        ]}
      />
      <ProFormText name="phoneNumber" label="手机号码" placeholder="请输入手机号" />
      {/* <ProFormSelect
        name="parkingLots"
        label="选择路段"
        showSearch
        mode="multiple"
        debounceTime={300}
        placeholder="请选择路段"
        rules={[
          {
            required: true,
            message: '请选择路段',
            validator: (_, val) => {
              if (!val || val.length <= 20) {
                return Promise.resolve(val);
              }
              return Promise.reject(new Error('执勤路段最多选择20个'));
            },
          },
        ]}
        request={(val) => queryParkinglotNames(val.keyWords)}
      /> */}
      {userRole === Role.ADMIN && (
        <ProFormSelect
          name="ownerId"
          label="用户归属公司"
          showSearch
          debounceTime={300}
          rules={[{ required: true, message: '请选择用户目标公司' }]}
          placeholder="请选择归属公司(如未找到目标公司，请优先创建[企业管理者])"
          request={(val) => queryCompanies(val.keyWords || ownerInfo?.ownerName, false)}
        />
      )}
    </ModalForm>
  );
};
